<!DOCTYPE html><html><head>
      <title>4&#x4F1A;&#x8BDD;&#x63A7;&#x5236;</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:///C:\Users\Administrator\.vscode\extensions\shd101wyy.markdown-preview-enhanced-0.4.3\node_modules\@shd101wyy\mume\dependencies\katex\katex.min.css">
      
      

      
      
      
      
      
      

      <style>
      /**
 * prism.js Github theme based on GitHub's theme.
 * @author Sam Clarke
 */
code[class*="language-"],
pre[class*="language-"] {
  color: #333;
  background: none;
  font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  text-align: left;
  white-space: pre;
  word-spacing: normal;
  word-break: normal;
  word-wrap: normal;
  line-height: 1.4;

  -moz-tab-size: 8;
  -o-tab-size: 8;
  tab-size: 8;

  -webkit-hyphens: none;
  -moz-hyphens: none;
  -ms-hyphens: none;
  hyphens: none;
}

/* Code blocks */
pre[class*="language-"] {
  padding: .8em;
  overflow: auto;
  /* border: 1px solid #ddd; */
  border-radius: 3px;
  /* background: #fff; */
  background: #f5f5f5;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
  padding: .1em;
  border-radius: .3em;
  white-space: normal;
  background: #f5f5f5;
}

.token.comment,
.token.blockquote {
  color: #969896;
}

.token.cdata {
  color: #183691;
}

.token.doctype,
.token.punctuation,
.token.variable,
.token.macro.property {
  color: #333;
}

.token.operator,
.token.important,
.token.keyword,
.token.rule,
.token.builtin {
  color: #a71d5d;
}

.token.string,
.token.url,
.token.regex,
.token.attr-value {
  color: #183691;
}

.token.property,
.token.number,
.token.boolean,
.token.entity,
.token.atrule,
.token.constant,
.token.symbol,
.token.command,
.token.code {
  color: #0086b3;
}

.token.tag,
.token.selector,
.token.prolog {
  color: #63a35c;
}

.token.function,
.token.namespace,
.token.pseudo-element,
.token.class,
.token.class-name,
.token.pseudo-class,
.token.id,
.token.url-reference .token.variable,
.token.attr-name {
  color: #795da3;
}

.token.entity {
  cursor: help;
}

.token.title,
.token.title .token.punctuation {
  font-weight: bold;
  color: #1d3e81;
}

.token.list {
  color: #ed6a43;
}

.token.inserted {
  background-color: #eaffea;
  color: #55a532;
}

.token.deleted {
  background-color: #ffecec;
  color: #bd2c00;
}

.token.bold {
  font-weight: bold;
}

.token.italic {
  font-style: italic;
}


/* JSON */
.language-json .token.property {
  color: #183691;
}

.language-markup .token.tag .token.punctuation {
  color: #333;
}

/* CSS */
code.language-css,
.language-css .token.function {
  color: #0086b3;
}

/* YAML */
.language-yaml .token.atrule {
  color: #63a35c;
}

code.language-yaml {
  color: #183691;
}

/* Ruby */
.language-ruby .token.function {
  color: #333;
}

/* Markdown */
.language-markdown .token.url {
  color: #795da3;
}

/* Makefile */
.language-makefile .token.symbol {
  color: #795da3;
}

.language-makefile .token.variable {
  color: #183691;
}

.language-makefile .token.builtin {
  color: #0086b3;
}

/* Bash */
.language-bash .token.keyword {
  color: #0086b3;
}

/* highlight */
pre[data-line] {
  position: relative;
  padding: 1em 0 1em 3em;
}
pre[data-line] .line-highlight-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  background-color: transparent;
  display: block;
  width: 100%;
}

pre[data-line] .line-highlight {
  position: absolute;
  left: 0;
  right: 0;
  padding: inherit 0;
  margin-top: 1em;
  background: hsla(24, 20%, 50%,.08);
  background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0));
  pointer-events: none;
  line-height: inherit;
  white-space: pre;
}

pre[data-line] .line-highlight:before, 
pre[data-line] .line-highlight[data-end]:after {
  content: attr(data-start);
  position: absolute;
  top: .4em;
  left: .6em;
  min-width: 1em;
  padding: 0 .5em;
  background-color: hsla(24, 20%, 50%,.4);
  color: hsl(24, 20%, 95%);
  font: bold 65%/1.5 sans-serif;
  text-align: center;
  vertical-align: .3em;
  border-radius: 999px;
  text-shadow: none;
  box-shadow: 0 1px white;
}

pre[data-line] .line-highlight[data-end]:after {
  content: attr(data-end);
  top: auto;
  bottom: .4em;
}html body{font-family:"Helvetica Neue",Helvetica,"Segoe UI",Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ul,html body>ol{margin-bottom:16px}html body ul,html body ol{padding-left:2em}html body ul.no-list,html body ol.no-list{padding:0;list-style-type:none}html body ul ul,html body ul ol,html body ol ol,html body ol ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:bold;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:bold}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em !important;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::before,html body code::after{letter-spacing:-0.2em;content:"\00a0"}html body pre>code{padding:0;margin:0;font-size:.85em !important;word-break:normal;white-space:pre;background:transparent;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;font-size:.85em !important;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:before,html body pre tt:before,html body pre code:after,html body pre tt:after{content:normal}html body p,html body blockquote,html body ul,html body ol,html body dl,html body pre{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body pre,html body code{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview .pagebreak,.markdown-preview .newpage{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center !important}.markdown-preview:not([for="preview"]) .code-chunk .btn-group{display:none}.markdown-preview:not([for="preview"]) .code-chunk .status{display:none}.markdown-preview:not([for="preview"]) .code-chunk .output-div{margin-bottom:16px}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0}@media screen and (min-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{font-size:14px !important;padding:1em}}@media print{html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,0.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{padding:0 1.6em;margin-top:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc li{margin-bottom:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{list-style-type:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% -  300px);padding:2em calc(50% - 457px -  150px);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
    </head>
    <body for="html-export">
      <div class="mume markdown-preview  ">
      <h1 class="mume-header" id="-%E4%B8%80-http-%E5%8D%8F%E8%AE%AE">:-: &#x4E00;&#x3001;http &#x534F;&#x8BAE;</h1>

<ul>
<li>http&#x662F;&#x65E0;&#x72B6;&#x6001;&#x7684;,&#x6BCF;&#x4E2A;&#x9875;&#x9762;&#x5B8C;&#x5168;&#x72EC;&#x7ACB;,&#x65E0;&#x6CD5;&#x901A;&#x4FE1;</li>
<li>http&#x65E0;&#x6CD5;&#x8DDF;&#x8E2A;&#x5F53;&#x524D;&#x8BBF;&#x95EE;&#x8005;&#x7684;,&#x65E0;&#x6CD5;&#x63D0;&#x4F9B;&#x4E2A;&#x6027;&#x5316;&#x7684;&#x5B9A;&#x5236;&#x670D;&#x52A1;,&#x4F8B;&#x5982;&#x767B;&#x5F55;,&#x8D2D;&#x7269;&#x8F66;&#x7B49;</li>
<li>&#x6709;&#x5FC5;&#x8981;&#x5728;&#x591A;&#x4E2A;&#x9875;&#x9762;&#x4E4B;&#x95F4;&#x5B58;&#x50A8;&#x4E00;&#x4E9B;&#x516C;&#x5171;&#x53D8;&#x91CF;,&#x4FDD;&#x5B58;&#x8BBF;&#x95EE;&#x8005;&#x7684;&#x4FE1;&#x606F;,&#x5C31;&#x663E;&#x793A;&#x975E;&#x5E38;&#x6709;&#x5FC5;&#x8981;</li>
</ul>
<hr>
<h1 class="mume-header" id="-%E4%BA%8C-cookie">:-: &#x4E8C;&#x3001;COOKIE</h1>

<ul>
<li>&#x670D;&#x52A1;&#x5668;&#x5BF9;&#x7528;&#x6237;&#x8BBF;&#x95EE;&#x7684;&#x8DDF;&#x8E2A;&#x624B;&#x6BB5;</li>
<li><code>$_COOKIE</code>: &#x8D85;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x6570;&#x7EC4;</li>
<li><code>setcookie()</code>: &#x8BBE;&#x7F6E;&#x5BA2;&#x6237;&#x7AEF;cookie</li>
<li>&#x5E38;&#x7528;&#x64CD;&#x4F5C;: &#x521B;&#x5EFA;/&#x8BFB;&#x53D6;/&#x66F4;&#x65B0;/&#x5220;&#x9664;</li>
<li>&#x6D4F;&#x89C8;&#x5668;&#x5F00;&#x53D1;&#x8005;&#x5DE5;&#x5177;&#xFF0C;application&#x4E2D;&#x67E5;&#x770B;<code>COOKIE</code></li>
</ul>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token shell-comment comment"># 1&#x3001;&#x8BBE;&#x7F6E;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x53EF;&#x8BBE;&#x7F6E;&#x8FC7;&#x671F;&#x65F6;&#x95F4;,&#x9ED8;&#x8BA4;&#x5173;&#x95ED;&#x9875;&#x9762;&#x81EA;&#x52A8;&#x6D88;&#x9664;</span>
<span class="token comment">// &#x8BBE;&#x7F6E;user_id&#x53D8;&#x91CF;,&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x4E00;&#x5206;&#x949F;&#x540E;&#x5931;&#x6548;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;&#x6B27;&#x9633;&#x514B;&apos;</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token shell-comment comment"># 2&#x3001;&#x8BFB;&#x53D6;</span>
<span class="token comment">// &#x9700;&#x8981;&#x5237;&#x65B0;&#x4E8C;&#x6B21;,&#x624D;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5230;cookie,&#x539F;&#x56E0;&#x662F;</span>
<span class="token comment">// &#x7B2C;&#x4E00;&#x6B21;&#x5237;&#x65B0;,&#x662F;&#x5C06;cookie&#x5199;&#x5165;&#x5230;&#x5BA2;&#x6237;&#x7AEF;, &#x5373;&#x5199;&#x64CD;&#x4F5C;</span>
<span class="token comment">// &#x7B2C;&#x4E8C;&#x6B21;&#x5237;&#x65B0;,&#x662F;&#x5C06;&#x5199;&#x5165;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x7684;cookie&#x6570;&#x636E;&#x8BFB;&#x51FA;&#x6765;,&#x5373;&#x8BFB;&#x64CD;&#x4F5C;</span>
<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;id = &apos;</span><span class="token punctuation">.</span> <span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;&lt;br&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;name = &apos;</span><span class="token punctuation">.</span> <span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;&lt;br&gt;&apos;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x5982;&#x679C;&#x60F3;&#x5728;&#x4E00;&#x4E2A;cookie&#x53D8;&#x91CF;&#x53EF;&#x5B58;&#x50A8;&#x591A;&#x4E2A;&#x503C;,&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x8BED;&#x6CD5;</span>
<span class="token comment">// 60*60&#x8868;&#x793A;1&#x5C0F;&#x65F6;, 60*60*24&#x8868;&#x793A;1&#x5929;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;user[id]&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;2&apos;</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;user[name]&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;&#x6B27;&#x9633;&#x514B;&apos;</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;user[email]&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;a@oyk.cn&apos;</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;user&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;user&apos;</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token variable">$key</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$key</span><span class="token punctuation">}</span></span> =&gt; <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span> &lt;br&gt;&quot;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token shell-comment comment"># 3&#x3001;&#x66F4;&#x65B0;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">800</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;id = &apos;</span><span class="token punctuation">.</span> <span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token shell-comment comment"># 4&#x3001;&#x5220;&#x9664;</span>
<span class="token function">unset</span><span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;username&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// &#x51FD;&#x6570;&#x5220;&#x9664;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;username&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;&apos;</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">3600</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// &#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x5DF2;&#x7ECF;&#x8FC7;&#x671F;&#x7684;&#x6301;&#x7EED;&#x65F6;&#x95F4;</span>
</pre><hr>
<h1 class="mume-header" id="-%E4%B8%89-session">:-: &#x4E09;&#x3001;SESSION</h1>

<ul>
<li><code>session_start()</code>: &#x542F;&#x52A8;&#x65B0;&#x4F1A;&#x8BDD;&#x6216;&#x8005;&#x91CD;&#x7528;&#x73B0;&#x6709;&#x4F1A;&#x8BDD;</li>
<li><code>session_id()</code>: &#x83B7;&#x53D6;/&#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x4F1A;&#x8BDD; ID</li>
<li><code>session_save_path()</code>: &#x8BFB;&#x53D6;/&#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x7684;&#x4FDD;&#x5B58;&#x8DEF;&#x5F84;</li>
<li><code>session_encode()</code>: &#x5C06;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x6570;&#x636E;&#x7F16;&#x7801;&#x4E3A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</li>
<li><code>session_decode</code>: &#x89E3;&#x7801;&#x4F1A;&#x8BDD;&#x6570;&#x636E;</li>
<li><code>session_destroy()</code>: &#x9500;&#x6BC1;&#x4E00;&#x4E2A;&#x4F1A;&#x8BDD;&#x4E2D;&#x7684;&#x5168;&#x90E8;&#x6570;&#x636E;,&#x4EC5;&#x6E05;&#x7A7A;&#x800C;&#x5DF2;</li>
<li><code>session_unset()</code>: &#x91CA;&#x653E;&#x6240;&#x6709;&#x7684;&#x4F1A;&#x8BDD;&#x53D8;&#x91CF;</li>
<li><code>session_reset()</code>: &#x56DE;&#x6EDA;&#x5230;&#x4E0A;&#x4E00;&#x6B21;&#x7684;&#x4F1A;&#x8BDD;</li>
</ul>
<p><strong>&#x6CE8;&#x610F;:</strong> &#x5FC5;&#x987B;&#x5148;&#x6267;&#x884C;<code>session_start()</code>&#x5F00;&#x542F;&#x4F1A;&#x8BDD;&#x624D;&#x751F;&#x6548;,&#x4E14;&#x4E4B;&#x524D;&#x4E0D;&#x80FD;&#x6709;&#x8F93;&#x51FA;</p>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token comment">// &#x5F00;&#x542F;&#x4E00;&#x4E2A;&#x4F1A;&#x8BDD;(&#x4E4B;&#x524D;&#x4E0D;&#x80FD;&#x6709;&#x8F93;&#x51FA;)</span>
<span class="token comment">// session_start()&#x81EA;&#x52A8;&#x8BBE;&#x7F6E;&#x5BA2;&#x6237;&#x7AEF;&#x7684;session_id,&#x6216;&#x91CD;&#x542F;&#x542F;&#x7528;&#x4E00;&#x4E2A;&#x5DF2;&#x5B58;&#x5728;&#x7684;&#x4F1A;&#x8BDD;</span>
<span class="token function">session_start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x5237;&#x65B0;&#x9875;&#x9762;, &#x6253;&#x5F00;application&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x5230;COOKIE&#x91CC;&#x6709;PHPSESSID,&#x9002;&#x7528;&#x4E8E;&#x6839;&#x8DEF;&#x5F84;</span>

<span class="token comment">// &#x67E5;&#x770B;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x7684;sesscion&#x4F1A;&#x8BDD;&#x5B58;&#x50A8;&#x7684;&#x8DEF;&#x5F84;&#x4F4D;&#x7F6E;</span>
<span class="token comment">// &#x5230;&#x8BE5;&#x8DEF;&#x5F84;&#x4E0B;&#x53EF;&#x770B;&#x5230;&#x4E00;&#x4E2A;sess_&#x4E3A;&#x524D;&#x7F00;,&#x540E;&#x8DDF;session_id&#x7684;&#x6587;&#x4EF6;&#x540D;</span>
<span class="token keyword">echo</span> <span class="token function">session_save_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;hr&gt;&apos;</span><span class="token punctuation">;</span>

<span class="token comment">// 1&#x3001;&#x8BBE;&#x7F6E;</span>
<span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
<span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token single-quoted-string string">&apos;&#x6B27;&#x9633;&#x514B;&apos;</span><span class="token punctuation">;</span>

<span class="token comment">// 2&#x3001;&#x8BFB;&#x53D6;</span>
<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;id = &apos;</span><span class="token punctuation">.</span> <span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;id&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;&lt;br&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;name = &apos;</span><span class="token punctuation">.</span> <span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;&lt;br&gt;&apos;</span><span class="token punctuation">;</span>

<span class="token comment">// 3&#x3001;&#x6E05;&#x9664;</span>
<span class="token function">session_unset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// &#x4F1A;&#x8BDD;&#x5185;&#x5BB9;&#x6E05;&#x7A7A;,&#x4F46;&#x4F1A;&#x8BDD;&#x6587;&#x4EF6;&apos;sess_******&apos;&#x4ECD;&#x5728;</span>
<span class="token function">session_destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// &#x4F1A;&#x8BDD;&#x5185;&#x5BB9;&#x6E05;&#x7A7A;,&#x4F1A;&#x8BDD;&#x6587;&#x4EF6;&#x5220;&#x9664;,&#x4F46;&#x5BA2;&#x6237;&#x7AEF;&#x7684;cookie&#x4ECD;&#x5728;,&#x5373;&#x4F1A;&#x8BDD;&#x4ECD;&#x5728;,&#x8FD8;&#x80FD;&#x91CD;&#x542F;</span>
<span class="token comment">// &#x53EF;&#x4EE5;session_destroy()&#x540E;, &#x518D;&#x8C03;&#x7528;setcookie()&#x6E05;&#x9664;&#x6389;&#x5BA2;&#x6237;&#x7AEF;&#x7684;session_id,&#x5C31;&#x5B8C;&#x5168;&#x6E05;&#x9664;&#x4E86;&#x4F1A;&#x8BDD;</span>
<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;PHPSESSID&apos;</span><span class="token punctuation">,</span><span class="token single-quoted-string string">&apos;&apos;</span><span class="token punctuation">,</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">3600</span><span class="token punctuation">,</span><span class="token single-quoted-string string">&apos;/&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</pre><hr>
<h1 class="mume-header" id="-%E5%9B%9B-cookie%E4%B8%8Esession">:-: &#x56DB;&#x3001;COOKIE&#x4E0E;SESSION</h1>

<ul>
<li>
<ol>
<li>cookie&#x4E0E;session&#x662F;php&#x4E2D;&#x975E;&#x5E38;&#x6709;&#x6548;&#x7684;&#x5728;&#x591A;&#x9875;&#x9762;&#x95F4;&#x8DDF;&#x8E2A;&#x7528;&#x6237;&#x7684;&#x624B;&#x6BB5;</li>
</ol>
</li>
<li>
<ol start="2">
<li>cookie&#x662F;&#x5C06;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x4FDD;&#x5B58;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;,&#x56E0;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x9650;&#x5236;,&#x5BB9;&#x91CF;&#x6709;&#x9650;,&#x4E14;&#x5E76;&#x4E0D;&#x5B89;&#x5168;</li>
</ol>
</li>
<li>
<ol start="3">
<li>session,&#x4E5F;&#x53EB;&#x4F1A;&#x8BDD;,&#x662F;&#x5C06;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x4FDD;&#x5B58;&#x5230;&#x670D;&#x52A1;&#x5668;&#x7AEF;,&#x5BB9;&#x91CF;&#x66F4;&#x5927;,&#x5E76;&#x4E14;&#x975E;&#x5E38;&#x5B89;&#x5168;</li>
</ol>
</li>
<li>
<ol start="4">
<li>&#x5176;&#x5B9E;session&#x4E5F;&#x662F;&#x57FA;&#x4E8E;cookie&#x7684;,&#x5C06;&#x6807;&#x8BC6;&#x7528;&#x6237;&#x8EAB;&#x4EFD;&#x7684;SESSION_ID&#x4FDD;&#x5B58;&#x5728;&#x5BA2;&#x6237;&#x7AEF;</li>
</ol>
</li>
<li>
<ol start="5">
<li>&#x6240;&#x4EE5;cookie&#x4E0E;session&#x5BC6;&#x4E0D;&#x53EF;&#x5206;,&#x5C3D;&#x7BA1;&#x5728;&#x64CD;&#x7528;session&#x65F6;,cookie&#x4F3C;&#x4E4E;&#x611F;&#x89C9;&#x4E0D;&#x5230;</li>
</ol>
</li>
</ul>
<hr>
<h1 class="mume-header" id="-%E4%BA%94-cookie%E5%AE%9E%E6%88%98">:-: &#x4E94;&#x3001;COOKIE&#x5B9E;&#x6218;</h1>

<ul>
<li>&#x4EE5;&#x7528;&#x6237;&#x767B;&#x9646;&#x4E3A;&#x4F8B;&#xFF0C;&#x4F7F;&#x7528;&#x6280;&#x672F;&#x6709;&#xFF1A;
<ul>
<li>&#x6570;&#x636E;&#x5E93;pdo&#x64CD;&#x4F5C;</li>
<li>&#x8BF7;&#x6C42;&#x6D3E;&#x53D1;&#x5668;</li>
<li>&#x9A8C;&#x8BC1;(&#x767B;&#x9646;&#x3001;&#x672A;&#x767B;&#x9646;&#x3001;&#x91CD;&#x590D;&#x767B;&#x9646;)</li>
</ul>
</li>
</ul>
<blockquote>
<p>&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x8868;</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token constant">CREATE</span> <span class="token constant">TABLE</span> `user` <span class="token punctuation">(</span>
	`uid` <span class="token function">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> unsigned <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token constant">AUTO_INCREMENT</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;ID&apos;</span><span class="token punctuation">,</span>
	`phone` <span class="token function">varchar</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;&#x624B;&#x673A;&#x53F7;&apos;</span><span class="token punctuation">,</span>
	`name` <span class="token function">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span> <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;&#x59D3;&#x540D;&apos;</span><span class="token punctuation">,</span>
	`pwd` <span class="token function">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;&#x5BC6;&#x7801;&apos;</span><span class="token punctuation">,</span>
	`age` <span class="token function">int</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> unsigned <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;18&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;&#x5E74;&#x9F84;&apos;</span><span class="token punctuation">,</span>
	`sex` <span class="token function">tinyint</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> unsigned <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;0&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;&#x6027;&#x522B;&#xFF0C;&#x503C;&#x4E3A;1&#x65F6;&#x662F;&#x7537;&#x6027;&#xFF0C;&#x503C;&#x4E3A;2&#x65F6;&#x662F;&#x5973;&#x6027;&#xFF0C;&#x503C;&#x4E3A;0&#x65F6;&#x662F;&#x672A;&#x77E5;&apos;</span><span class="token punctuation">,</span>
	`status` <span class="token function">tinyint</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> unsigned <span class="token constant">NOT</span> <span class="token keyword">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;1&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;1&#x4E3A;&#x6B63;&#x5E38;&#xFF0C;0&#x4E3A;&#x7981;&#x6B62;&apos;</span><span class="token punctuation">,</span>
	`last_time` <span class="token function">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> unsigned <span class="token keyword">DEFAULT</span> <span class="token single-quoted-string string">&apos;0&apos;</span> <span class="token constant">COMMENT</span> <span class="token single-quoted-string string">&apos;&#x6700;&#x540E;&#x767B;&#x5F55;&#x65F6;&#x95F4;&apos;</span><span class="token punctuation">,</span>
  <span class="token constant">PRIMARY</span> <span class="token function">KEY</span> <span class="token punctuation">(</span>`uid`<span class="token punctuation">)</span> <span class="token constant">USING</span> <span class="token constant">BTREE</span>
<span class="token punctuation">)</span> <span class="token constant">ENGINE</span><span class="token operator">=</span>MyISAM <span class="token constant">AUTO_INCREMENT</span><span class="token operator">=</span><span class="token number">17</span> <span class="token keyword">DEFAULT</span> <span class="token constant">CHARSET</span><span class="token operator">=</span>utf8 <span class="token constant">COMMENT</span><span class="token operator">=</span><span class="token single-quoted-string string">&apos;&#x7528;&#x6237;--&#x7528;&#x6237;&#x4FE1;&#x606F;&#x8868;&apos;</span><span class="token punctuation">;</span>

<span class="token constant">INSERT</span> <span class="token constant">INTO</span> `user` <span class="token function">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;18011112222&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;&#x6B27;&#x9633;&#x514B;&apos;</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;e10adc3949ba59abbe56e057f20f883e&apos;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1561125595</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</pre><blockquote>
<p>&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x6587;&#x4EF6;&#xFF1A;connect.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$db</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
	<span class="token single-quoted-string string">&apos;type&apos;</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token single-quoted-string string">&apos;mysql&apos;</span><span class="token punctuation">,</span>
	<span class="token single-quoted-string string">&apos;host&apos;</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token single-quoted-string string">&apos;localhost&apos;</span><span class="token punctuation">,</span>
	<span class="token single-quoted-string string">&apos;dbname&apos;</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token single-quoted-string string">&apos;php&apos;</span><span class="token punctuation">,</span>
	<span class="token single-quoted-string string">&apos;username&apos;</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token single-quoted-string string">&apos;root&apos;</span><span class="token punctuation">,</span>
	<span class="token single-quoted-string string">&apos;password&apos;</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token single-quoted-string string">&apos;root&apos;</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token variable">$dsn</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$db</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;type&apos;</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span>:host=<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$db</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;host&apos;</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span>;dbname=<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$db</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;dbname&apos;</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span>&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token variable">$db</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;username&apos;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token variable">$db</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;password&apos;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
	<span class="token variable">$pdo</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token variable">$dsn</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>PDOException <span class="token variable">$e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">die</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;&#x8FDE;&#x63A5;&#x5931;&#x8D25;&apos;</span> <span class="token punctuation">.</span> <span class="token variable">$e</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x9996;&#x9875;&#x6587;&#x4EF6;&#xFF1A;index.php<br>
&#x5148;&#x5C55;&#x793A;&#x672A;&#x767B;&#x9646;&#x529F;&#x80FD;</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// &#x4E3A;&#x7B80;&#x5316;&#x7A0B;&#x5E8F;, &#x4F7F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x5C42;(dispatch.php): &#x8BF7;&#x6C42;&#x6D3E;&#x53D1;&#x5668;,&#x7C7B;&#x4F3C;&#x4E8E;&#x6846;&#x67B6;&#x7684;&#x63A7;&#x5236;&#x5668;, &#x5BF9;&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;&#x8FDB;&#x884C;&#x96C6;&#x4E2D;&#x5904;&#x7406;</span>

<span class="token comment">// 1: &#x5DF2;&#x767B;&#x5F55;: &#x663E;&#x793A;&#x51FA;&#x7528;&#x6237;&#x7684;&#x767B;&#x5F55;&#x4FE1;&#x606F;, &#x663E;&#x793A;&#x9000;&#x51FA;&#x6309;&#x94AE;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&#x7528;&#x6237;: &apos;</span> <span class="token punctuation">.</span> <span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;&#x5DF2;&#x767B;&#x5F55;&lt;br&gt;&apos;</span><span class="token punctuation">;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;a href=&quot;dispatch.php?action=logout&quot;&gt;&#x9000;&#x51FA;&lt;/a&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
	<span class="token comment">// 2. &#x672A;&#x767B;&#x5F55;,&#x5C31;&#x8DF3;&#x8F6C;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;a href=&quot;dispatch.php?action=login&quot;&gt;&#x8BF7;&#x767B;&#x5F55;&lt;/a&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x6D3E;&#x53D1;&#x5668;&#xFF1A;dispatch.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// &#x8BF7;&#x6C42;&#x6D3E;&#x53D1;&#x5668;: &#x524D;&#x7AEF;&#x63A7;&#x5236;&#x5668;</span>
<span class="token comment">// &#x529F;&#x80FD;&#x5C31;&#x662F;&#x83B7;&#x53D6;&#x5230;&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;, &#x5E76;&#x8C03;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x811A;&#x672C;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x548C;&#x54CD;&#x5E94;</span>

<span class="token comment">// &#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</span>
<span class="token keyword">require</span> <span class="token constant">__DIR__</span>  <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/connect.php&apos;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x53C2;&#x6570;</span>
<span class="token variable">$action</span> <span class="token operator">=</span> <span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;action&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;action&apos;</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token single-quoted-string string">&apos;login&apos;</span><span class="token punctuation">;</span>
<span class="token variable">$action</span> <span class="token operator">=</span> <span class="token function">htmlentities</span><span class="token punctuation">(</span><span class="token function">strtolower</span><span class="token punctuation">(</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token variable">$action</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x8BF7;&#x6C42;&#x5206;&#x53D1;</span>
<span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token variable">$action</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. &#x767B;&#x5F55;&#x9875;&#x9762;</span>
	<span class="token keyword">case</span> <span class="token single-quoted-string string">&apos;login&apos;</span><span class="token punctuation">:</span>
		<span class="token comment">// &#x52A0;&#x8F7D;&#x767B;&#x5F55;&#x8868;&#x5355;</span>
		<span class="token keyword">include</span> <span class="token constant">__DIR__</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/login.php&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>

	<span class="token comment">// 2. &#x9A8C;&#x8BC1;&#x767B;&#x5F55;</span>
	<span class="token keyword">case</span> <span class="token single-quoted-string string">&apos;check&apos;</span><span class="token punctuation">:</span>
		<span class="token keyword">include</span> <span class="token constant">__DIR__</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/check.php&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>

	<span class="token comment">// 3. &#x9000;&#x51FA;&#x767B;&#x5F55;</span>
	<span class="token keyword">case</span> <span class="token single-quoted-string string">&apos;logout&apos;</span><span class="token punctuation">:</span>
		<span class="token keyword">include</span> <span class="token constant">__DIR__</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/logout.php&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>

	<span class="token comment">// &#x9ED8;&#x8BA4;&#x64CD;&#x4F5C;</span>
	<span class="token keyword">default</span><span class="token punctuation">:</span>
		<span class="token function">header</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;Location: index.php&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x767B;&#x9646;&#x9875;&#x9762;&#xFF1A;login.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
	<span class="token comment">// &#x9632;&#x6B62;&#x7528;&#x6237;&#x91CD;&#x590D;&#x767B;&#x5F55;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x4E0D;&#x8981;&#x91CD;&#x590D;&#x767B;&#x5F55;&quot;);location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token delimiter important">?&gt;</span>

<span class="token operator">&lt;</span><span class="token operator">!</span>doctype html<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>html lang<span class="token operator">=</span><span class="token double-quoted-string string">&quot;en&quot;</span><span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span>head<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>meta charset<span class="token operator">=</span><span class="token double-quoted-string string">&quot;UTF-8&quot;</span><span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>title<span class="token operator">&gt;</span>&#x7528;&#x6237;&#x767B;&#x5F55;<span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span>body<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>h3<span class="token operator">&gt;</span>&#x7528;&#x6237;&#x767B;&#x5F55;<span class="token operator">&lt;</span><span class="token operator">/</span>h3<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token double-quoted-string string">&quot;dispatch.php?action=check&quot;</span> method<span class="token operator">=</span><span class="token double-quoted-string string">&quot;post&quot;</span> onsubmit<span class="token operator">=</span><span class="token double-quoted-string string">&quot;return isEmpty();&quot;</span><span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>label <span class="token keyword">for</span><span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span><span class="token operator">&gt;</span>&#x624B;&#x673A;&#x53F7;<span class="token punctuation">:</span><span class="token operator">&lt;</span><span class="token operator">/</span>label<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span> name<span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span> id<span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span><span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>

			<span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>label <span class="token keyword">for</span><span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span><span class="token operator">&gt;</span>&#x5BC6;&#x7801;<span class="token punctuation">:</span><span class="token operator">&lt;</span><span class="token operator">/</span>label<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span> name<span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span> id<span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span><span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>

			<span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>button<span class="token operator">&gt;</span>&#x63D0;&#x4EA4;<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">&gt;</span>

		<span class="token operator">&lt;</span>script<span class="token operator">&gt;</span>
			<span class="token keyword">function</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token keyword">var</span> phone <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;phone&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>
				<span class="token keyword">var</span> password <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;password&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>

				<span class="token keyword">if</span> <span class="token punctuation">(</span>phone<span class="token punctuation">.</span>length<span class="token operator">===</span> <span class="token number">0</span> <span class="token operator">||</span> password<span class="token punctuation">.</span>length<span class="token operator">===</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
					<span class="token function">alert</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;&#x624B;&#x673A;&#x548C;&#x5BC6;&#x7801;&#x4E0D;&#x80FD;&#x4E3A;&#x7A7A;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
				<span class="token punctuation">}</span>
			<span class="token punctuation">}</span>
		<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">&gt;</span>
</pre><blockquote>
<p>&#x9A8C;&#x8BC1;&#x767B;&#x9646;&#xFF1A;check.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// 1.&#x5224;&#x65AD;&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x6B63;&#x786E;?</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;REQUEST_METHOD&apos;</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token single-quoted-string string">&apos;POST&apos;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

	<span class="token comment">// 2.&#x83B7;&#x53D6;&#x8868;&#x5355;&#x6570;&#x636E;</span>
	<span class="token variable">$phone</span> <span class="token operator">=</span> <span class="token global">$_POST</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;phone&apos;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">sha1</span><span class="token punctuation">(</span><span class="token global">$_POST</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;password&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">// 3. &#x7528;&#x7528;&#x6237;&#x8868;user.dbf&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;</span>
	<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token single-quoted-string string">&apos;SELECT * FROM `user` WHERE `phone` = :phone AND `password` = :password LIMIT 1&apos;</span><span class="token punctuation">;</span>
	<span class="token variable">$stmt</span> <span class="token operator">=</span> <span class="token variable">$pdo</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;phone&apos;</span><span class="token operator">=</span><span class="token operator">&gt;</span><span class="token variable">$phone</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;password&apos;</span><span class="token operator">=</span><span class="token operator">&gt;</span><span class="token variable">$password</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token variable">$user</span> <span class="token operator">=</span> <span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token scope">PDO<span class="token punctuation">::</span></span><span class="token constant">FETCH_ASSOC</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">// 4. &#x5224;&#x65AD;&#x9A8C;&#x8BC1;&#x7684;&#x7ED3;&#x679C;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token boolean">false</span> <span class="token operator">===</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// &#x9A8C;&#x8BC1;&#x5931;&#x8D25;,&#x8FD4;&#x56DE;&#x4E0A;&#x4E00;&#x4E0B;&#x9875;&#x9762;</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x9A8C;&#x8BC1;&#x5931;&#x8D25;&quot;);history.back();&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">die</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// &#x9A8C;&#x8BC1;&#x6210;&#x529F;&#xFF0C;&#x5C06;&#x7528;&#x6237;&#x7684;&#x4FE1;&#x606F;&#x5199;&#x5230;cookie</span>
	<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x767B;&#x5F55;&#x6210;&#x529F;&quot;);location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
	exit<span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
	<span class="token keyword">die</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;&#x8BF7;&#x6C42;&#x7C7B;&#x578B;&#x9519;&#x8BEF;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x9000;&#x51FA;&#x767B;&#x9646;&#xFF1A;logout.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// &#x5FC5;&#x987B;&#x5728;&#x7528;&#x6237;&#x5DF2;&#x7ECF;&#x767B;&#x5F55;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x518D;&#x9000;&#x51FA;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_COOKIE</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">setcookie</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">3600</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x9000;&#x51FA;&#x6210;&#x529F;&quot;);location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
	<span class="token comment">// &#x8981;&#x6C42;&#x7528;&#x6237;&#x5148;&#x767B;&#x5F55;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x8BF7;&#x5148;&#x767B;&#x5F55;&quot;);location.assign(&quot;login.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><hr>
<h1 class="mume-header" id="-%E5%85%AD-session%E5%AE%9E%E6%88%98">:-: &#x516D;&#x3001;SESSION&#x5B9E;&#x6218;</h1>

<ul>
<li>&#x6570;&#x636E;&#x5E93;&#x7EE7;&#x7EED;&#x4F7F;&#x7528;user</li>
</ul>
<blockquote>
<p>&#x9996;&#x9875;&#x6587;&#x4EF6;&#xFF1A;index.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
	<span class="token comment">//&#x5F00;&#x542F;&#x4F1A;&#x8BDD;</span>
	<span class="token function">session_start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">// &#x4E3A;&#x7B80;&#x5316;&#x7A0B;&#x5E8F;, &#x4F7F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x5C42;: &#x8BF7;&#x6C42;&#x6D3E;&#x53D1;&#x5668;,&#x7C7B;&#x4F3C;&#x4E8E;&#x6846;&#x67B6;&#x7684;&#x63A7;&#x5236;&#x5668;, &#x5BF9;&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;&#x8FDB;&#x884C;&#x96C6;&#x4E2D;&#x5904;&#x7406;</span>

	<span class="token comment">// 1: &#x5DF2;&#x767B;&#x5F55;: &#x663E;&#x793A;&#x51FA;&#x7528;&#x6237;&#x7684;&#x767B;&#x5F55;&#x4FE1;&#x606F;, &#x663E;&#x793A;&#x9000;&#x51FA;&#x6309;&#x94AE;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token single-quoted-string string">&apos;admin&apos;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&#x7528;&#x6237;: &apos;</span> <span class="token punctuation">.</span> <span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;&#x5DF2;&#x767B;&#x5F55;&lt;br&gt;&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;a href=&quot;dispatch.php?action=logout&quot;&gt;&#x9000;&#x51FA;&lt;/a&gt;&apos;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
		<span class="token comment">// 2. &#x672A;&#x767B;&#x5F55;,&#x5C31;&#x8DF3;&#x8F6C;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;a href=&quot;dispatch.php?action=login&quot;&gt;&#x8BF7;&#x767B;&#x5F55;&lt;/a&gt;&apos;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x6D3E;&#x53D1;&#x5668;&#xFF1A;dispatch.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// &#x53EA;&#x9700;&#x8981;&#x5728;&#x8BE5;&#x811A;&#x672C;&#x4E2D;&#x6253;&#x5F00;&#x4F1A;&#x8BDD;&#x5373;&#x53EF;, check.php/logout.php/login.php&#x90FD;&#x662F;&#x7531;&#x5B83;&#x8C03;&#x7528;&#x7684;, &#x4E0D;&#x5FC5;&#x91CD;&#x590D;&#x5F00;&#x542F;</span>
<span class="token function">session_start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</span>
<span class="token keyword">require</span> <span class="token constant">__DIR__</span>  <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/connect.php&apos;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x53C2;&#x6570;</span>
<span class="token variable">$action</span> <span class="token operator">=</span> <span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;action&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token global">$_GET</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;action&apos;</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token single-quoted-string string">&apos;login&apos;</span><span class="token punctuation">;</span>
<span class="token variable">$action</span> <span class="token operator">=</span> <span class="token function">htmlentities</span><span class="token punctuation">(</span><span class="token function">strtolower</span><span class="token punctuation">(</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token variable">$action</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x8BF7;&#x6C42;&#x5206;&#x53D1;</span>
<span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token variable">$action</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. &#x767B;&#x5F55;&#x9875;&#x9762;</span>
	<span class="token keyword">case</span> <span class="token single-quoted-string string">&apos;login&apos;</span><span class="token punctuation">:</span>
		<span class="token comment">// &#x52A0;&#x8F7D;&#x767B;&#x5F55;&#x8868;&#x5355;</span>
		<span class="token keyword">include</span> <span class="token constant">__DIR__</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/login.php&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>

	<span class="token comment">// 2. &#x9A8C;&#x8BC1;&#x767B;&#x5F55;</span>
	<span class="token keyword">case</span> <span class="token single-quoted-string string">&apos;check&apos;</span><span class="token punctuation">:</span>
		<span class="token keyword">include</span> <span class="token constant">__DIR__</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/check.php&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>

	<span class="token comment">// 3. &#x9000;&#x51FA;&#x767B;&#x5F55;</span>
	<span class="token keyword">case</span> <span class="token single-quoted-string string">&apos;logout&apos;</span><span class="token punctuation">:</span>
		<span class="token keyword">include</span> <span class="token constant">__DIR__</span> <span class="token punctuation">.</span> <span class="token single-quoted-string string">&apos;/logout.php&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>

	<span class="token comment">// &#x9ED8;&#x8BA4;&#x64CD;&#x4F5C;</span>
	<span class="token keyword">default</span><span class="token punctuation">:</span>
		<span class="token function">header</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;Location: index.php&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x767B;&#x9646;&#x9875;&#x9762;&#xFF1A;login.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
	<span class="token comment">// &#x9632;&#x6B62;&#x7528;&#x6237;&#x91CD;&#x590D;&#x767B;&#x5F55;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x4E0D;&#x8981;&#x91CD;&#x590D;&#x767B;&#x5F55;&quot;);location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token delimiter important">?&gt;</span>

<span class="token operator">&lt;</span><span class="token operator">!</span>doctype html<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>html lang<span class="token operator">=</span><span class="token double-quoted-string string">&quot;en&quot;</span><span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span>head<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>meta charset<span class="token operator">=</span><span class="token double-quoted-string string">&quot;UTF-8&quot;</span><span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>title<span class="token operator">&gt;</span>&#x7528;&#x6237;&#x767B;&#x5F55;<span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span>body<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>h3<span class="token operator">&gt;</span>&#x7528;&#x6237;&#x767B;&#x5F55;<span class="token operator">&lt;</span><span class="token operator">/</span>h3<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token double-quoted-string string">&quot;dispatch.php?action=check&quot;</span> method<span class="token operator">=</span><span class="token double-quoted-string string">&quot;post&quot;</span> onsubmit<span class="token operator">=</span><span class="token double-quoted-string string">&quot;return isEmpty();&quot;</span><span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>label <span class="token keyword">for</span><span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span><span class="token operator">&gt;</span>&#x624B;&#x673A;<span class="token punctuation">:</span><span class="token operator">&lt;</span><span class="token operator">/</span>label<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span> name<span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span> id<span class="token operator">=</span><span class="token double-quoted-string string">&quot;phone&quot;</span><span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>

			<span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>label <span class="token keyword">for</span><span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span><span class="token operator">&gt;</span>&#x5BC6;&#x7801;<span class="token punctuation">:</span><span class="token operator">&lt;</span><span class="token operator">/</span>label<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span> name<span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span> id<span class="token operator">=</span><span class="token double-quoted-string string">&quot;password&quot;</span><span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>

			<span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span>button<span class="token operator">&gt;</span>&#x63D0;&#x4EA4;<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">&gt;</span>

		<span class="token operator">&lt;</span>script<span class="token operator">&gt;</span>
			<span class="token keyword">function</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token keyword">var</span> phone <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;phone&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>
				<span class="token keyword">var</span> password <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;password&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>

				<span class="token keyword">if</span> <span class="token punctuation">(</span>phone<span class="token punctuation">.</span>length<span class="token operator">===</span> <span class="token number">0</span> <span class="token operator">||</span> password<span class="token punctuation">.</span>length<span class="token operator">===</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
					<span class="token function">alert</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;&#x624B;&#x673A;&#x548C;&#x5BC6;&#x7801;&#x4E0D;&#x80FD;&#x4E3A;&#x7A7A;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
				<span class="token punctuation">}</span>
			<span class="token punctuation">}</span>
		<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">&gt;</span>
</pre><blockquote>
<p>&#x9A8C;&#x8BC1;&#x767B;&#x9646;&#xFF1A;check.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// 1.&#x5224;&#x65AD;&#x7528;&#x6237;&#x7684;&#x8BF7;&#x6C42;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x6B63;&#x786E;?</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token global">$_SERVER</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;REQUEST_METHOD&apos;</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token single-quoted-string string">&apos;POST&apos;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 2.&#x83B7;&#x53D6;&#x8868;&#x5355;&#x6570;&#x636E;</span>
	<span class="token variable">$phone</span> <span class="token operator">=</span> <span class="token global">$_POST</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;phone&apos;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">sha1</span><span class="token punctuation">(</span><span class="token global">$_POST</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;password&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">// 3. &#x7528;&#x7528;&#x6237;&#x8868;user.dbf&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;</span>
	<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token single-quoted-string string">&apos;SELECT * FROM `user` WHERE `phone` = :phone AND `password` = :password LIMIT 1&apos;</span><span class="token punctuation">;</span>
	<span class="token variable">$stmt</span> <span class="token operator">=</span> <span class="token variable">$pdo</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;phone&apos;</span><span class="token operator">=</span><span class="token operator">&gt;</span><span class="token variable">$phone</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">&apos;password&apos;</span><span class="token operator">=</span><span class="token operator">&gt;</span><span class="token variable">$password</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token variable">$user</span> <span class="token operator">=</span> <span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token scope">PDO<span class="token punctuation">::</span></span><span class="token constant">FETCH_ASSOC</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">// 4. &#x5224;&#x65AD;&#x9A8C;&#x8BC1;&#x7684;&#x7ED3;&#x679C;</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token boolean">false</span> <span class="token operator">===</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// &#x9A8C;&#x8BC1;&#x5931;&#x8D25;,&#x8FD4;&#x56DE;&#x4E0A;&#x4E00;&#x4E0B;&#x9875;&#x9762;</span>
		<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x9A8C;&#x8BC1;&#x5931;&#x8D25;&quot;);history.back();&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
		<span class="token keyword">die</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// &#x9A8C;&#x8BC1;&#x6210;&#x529F;&#xFF0C;&#x5C06;&#x7528;&#x6237;&#x7684;&#x4FE1;&#x606F;&#x5199;&#x5230;session</span>
	<span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x767B;&#x5F55;&#x6210;&#x529F;&quot;);location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
	exit<span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
	<span class="token keyword">die</span><span class="token punctuation">(</span><span class="token single-quoted-string string">&apos;&#x8BF7;&#x6C42;&#x7C7B;&#x578B;&#x9519;&#x8BEF;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre><blockquote>
<p>&#x9000;&#x51FA;&#x767B;&#x9646;&#xFF1A;logout.php</p>
</blockquote>
<pre data-role="codeBlock" data-info="php" class="language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// &#x5FC5;&#x987B;&#x5728;&#x7528;&#x6237;&#x5DF2;&#x7ECF;&#x767B;&#x5F55;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x518D;&#x9000;&#x51FA;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token global">$_SESSION</span><span class="token punctuation">[</span><span class="token single-quoted-string string">&apos;name&apos;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">session_destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x9000;&#x51FA;&#x6210;&#x529F;&quot;);location.assign(&quot;index.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
	<span class="token comment">// &#x8981;&#x6C42;&#x7528;&#x6237;&#x5148;&#x767B;&#x5F55;</span>
	<span class="token keyword">echo</span> <span class="token single-quoted-string string">&apos;&lt;script&gt;alert(&quot;&#x8BF7;&#x5148;&#x767B;&#x5F55;&quot;);location.assign(&quot;login.php&quot;);&lt;/script&gt;&apos;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre>
      </div>
      
      
    
    
    
    
    
    
    
    
  
    </body></html>